fix: handle undefined ephemeralMessage for iOS PTT audio messages#2552
fix: handle undefined ephemeralMessage for iOS PTT audio messages#2552gabrielgz0 wants to merge 1 commit into
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds defensive checks and safer unwrapping for WhatsApp Baileys ephemeral audio messages to prevent crashes when File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
if (!msg.message)check appears redundant with the existingif (!msg?.message)guard just above it; consider consolidating to a single null/undefined check to avoid duplication. - Since you're now depending on
msg.message[subtype]?.messagespecifically, it might be safer to narrow the loop to only subtypes known to wrap a.messageto avoid silently skipping other container shapes in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `if (!msg.message)` check appears redundant with the existing `if (!msg?.message)` guard just above it; consider consolidating to a single null/undefined check to avoid duplication.
- Since you're now depending on `msg.message[subtype]?.message` specifically, it might be safer to narrow the loop to only subtypes known to wrap a `.message` to avoid silently skipping other container shapes in the future.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…4FromMediaMessage
c69d13d to
75506ca
Compare
|
🟢 👍 Na |
dpaes
left a comment
There was a problem hiding this comment.
Approved.
Reviewed independently against develop:
MessageSubtype(ephemeralMessage / viewOnce / documentWithCaption) are wrappers;TypeMediaMessage(audioMessage etc.) are leaves — no overlap. For a non-ephemeral message,msg.messageexists and the loop matches no subtype, so behavior is identical to before.- For the broken iOS case (
ephemeralMessagetruthy without nested.message), the old code setmsg.message = undefinedand crashed on the next iteration; the?.messagenow skips it. No legitimate unwrap is lost. - Note: the applied diff uses
!msg?.message(more robust than the!msg.messagedescribed in the PR body) — it also guards a null/undefinedmsg.
Focused, additive fix (+2/-2). LGTM 👍
Non-blocking suggestion for a follow-up commit (not required to merge): the guard throws a raw string (throw 'Message not found'). It works because the outer catch handles it, and it matches the existing pattern in this function — but for consistency with the rest of the codebase consider throwing a proper exception class (e.g. new BadRequestException('Message not found')) so the error carries a status/type. Just a hygiene nit you could fold into your next commit.
Summary
Fix a crash in
POST /chat/getBase64FromMediaMessage/{instance}when processing iOS audio messages wrapped inephemeralMessagewithout the expected.messageproperty.Problem
When an iPhone sends an audio message, the Baileys
messages.upsertevent delivers it wrapped in anephemeralMessagecontainer. The service iterates over knownMessageSubtypevalues and unwraps them:iOS ephemeral messages can have
ephemeralMessageas a truthy object without a standard.messageproperty inside. When that happens:msg.message = undefined(.messagedoesn't exist on the subtype)msg.message[subtype]->TypeError: Cannot read properties of undefined (reading 'ephemeralMessage')This also crashes when
msg.messageitself isundefined(e.g., passing a message object without valid content).Error response
{"status":400,"error":"Bad Request","response":{"message":["TypeError: Cannot read properties of undefined (reading 'ephemeralMessage')"]}}Changes
File:
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.tsmsg.message-- returns"Message not found"instead of crashingmsg.message[subtype]?.messageensures unwrapping only happens when.messageactually exists, preventingmsg.messagefrom being set toundefinedTesting
Scenarios verified
ephemeralMessagesem.message"The message is not of the media type"message: null"Message not found"messageContextInfoapenasnull(skip)"Message not found"ephemeralMessagecom audio validoaudioMessagedireto (DB format)Checks
npm run lint:checkcleantsc --noEmitcleanCloses #2550
Summary by Sourcery
Bug Fixes: